package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.db.NewBssDB;
import com.xforceplus.bsstool.db.OldBssDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep5 {

  /**
   * 获取老运营租户公司关联关系列表
   * @return
   */
  public static List<Map> getOldTenantCompanyRelList(Long tenantId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_group_company_rel t where t.group_id = ")
            .append(tenantId);
    logger.info("执行老运营租户公司关联关系获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营租户公司关联关系列表
   * @return
   */
  public static Map<String,Map> getNewTenantCompanyRelMap(Long tenantId,LogMain logger) throws Exception {
    Map<String,Map> newTenantCompanyRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_tenant_company_rel t where t.tenant_id = ")
            .append(tenantId);
    logger.info("执行新运营租户公司关联关系获取，执行SQL: "+sql.toString());
    List<Map> newTenantCompanyRelList = NewBssDB.getSqlResult(sql.toString());
    for (Map newTenantCompanyRelMap : newTenantCompanyRelList){
      newTenantCompanyRelMapMap.put(String.valueOf(newTenantCompanyRelMap.get("id")),newTenantCompanyRelMap);
    }
    return newTenantCompanyRelMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareTenantCompanyRelWithNew(List<Map> oldTenantCompanyRelList,Long tenantId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newTenantCompanyRelMapMap  = getNewTenantCompanyRelMap(tenantId,logger);
    for (Map oldTenantCompanyRelMap : oldTenantCompanyRelList){
      System.out.println("开始执行租户公司关联关系比对 租户公司关联关系ID:"+oldTenantCompanyRelMap.get("id"));
      logger.info("开始执行租户公司关联关系比对 租户公司关联关系ID:"+oldTenantCompanyRelMap.get("id"));
      //查询新运营对应功能集
      Map newTenantCompanyRelMap =newTenantCompanyRelMapMap.get(String.valueOf(oldTenantCompanyRelMap.get("id")));
      if (newTenantCompanyRelMap == null){
        logger.warn("租户公司关联关系ID:"+oldTenantCompanyRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同租户公司关联关系ID,继续执行数据信息比对 新运营租户公司关联关系："+ JSON.toJSONString(newTenantCompanyRelMap));
        //新老数据比对： tenant_id company_id
        oldTenantCompanyRelMap.put("tenant_id",oldTenantCompanyRelMap.get("group_id"));
        result = ServiceCommon.compareMapValue(oldTenantCompanyRelMap,newTenantCompanyRelMap,
                Lists.newArrayList("tenant_id","company_id"),
                "租户公司关联关系ID:"+oldTenantCompanyRelMap.get("id"),
                logger);
      }

      //重复租户公司关联关系检查
      List<Map> sameTenantCompanyRelList = NewBssDB.getSqlResult("select * from bss_tenant_company_rel where id != "+oldTenantCompanyRelMap.get("id")
              +" and tenant_id = "+oldTenantCompanyRelMap.get("tenant_id")
              +" and company_id = "+oldTenantCompanyRelMap.get("company_id"));
      if (!CollectionUtils.isEmpty(sameTenantCompanyRelList)){
        logger.error("老运营租户公司关联关系ID:"+oldTenantCompanyRelMap.get("id")+" 新运营存在重复租户公司关联关系记录个数："+sameTenantCompanyRelList.size());
        result = false;
      }
    }
    return result;
  }


  /**
   * 获取老运营公司列表
   * @return
   */
  public static List<Map> getOldCompanyList(List<Long> companyIdList, LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * from bss_company t where t.company_id ")
            .append(ServiceCommon.getCompanyIdInSql(companyIdList));
    logger.info("执行老运营公司获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营公司列表
   * @return
   */
  public static Map<String,Map> getNewCompanyMap(List<Long> companyIdList,LogMain logger) throws Exception {
    Map<String,Map> newCompanyMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * from bss_company t where t.company_id ")
            .append(ServiceCommon.getCompanyIdInSql(companyIdList));
    logger.info("执行新运营公司获取，执行SQL: "+sql.toString());
    List<Map> newCompanyList = NewBssDB.getSqlResult(sql.toString());
    for (Map newCompanyMap : newCompanyList){
      newCompanyMapMap.put(String.valueOf(newCompanyMap.get("company_id")),newCompanyMap);
    }
    return newCompanyMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareCompanyWithNew(List<Map> oldCompanyList,List<Long> companyIdList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newCompanyMapMap  = getNewCompanyMap(companyIdList,logger);
    for (Map oldCompanyMap : oldCompanyList){
      System.out.println("开始执行公司比对 公司ID:"+oldCompanyMap.get("company_id"));
      logger.info("开始执行公司比对 公司ID:"+oldCompanyMap.get("company_id"));
      //查询新运营对应功能集
      Map newCompanyMap =newCompanyMapMap.get(String.valueOf(oldCompanyMap.get("company_id")));
      if (newCompanyMap == null){
        logger.warn("公司ID:"+oldCompanyMap.get("company_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同公司ID,继续执行数据信息比对 新运营公司："+ JSON.toJSONString(newCompanyMap));
        //新老数据比对： tax_num  company_name  status
        result = ServiceCommon.compareMapValue(oldCompanyMap,newCompanyMap,
                Lists.newArrayList("tax_num","company_name","status"),
                "公司ID:"+oldCompanyMap.get("company_id"),
                logger);
      }

      //重复公司检查
      List<Map> sameCompanyList = NewBssDB.getSqlResult("select * from bss_company where company_id != "+oldCompanyMap.get("company_id")
              +" and tax_num = '"+oldCompanyMap.get("tax_num")+"'");
      if (!CollectionUtils.isEmpty(sameCompanyList)){
        logger.error("老运营公司ID:"+oldCompanyMap.get("company_id")+" 新运营存在重复公司(tax_num重复)记录个数："+sameCompanyList.size());
        result = false;
      }
      sameCompanyList = NewBssDB.getSqlResult("select * from bss_company where company_id != "+oldCompanyMap.get("company_id")
              +" and company_name = '"+oldCompanyMap.get("company_name")+"'");
      if (!CollectionUtils.isEmpty(sameCompanyList)){
        logger.error("老运营公司ID:"+oldCompanyMap.get("company_id")+" 新运营存在重复公司(company_name重复)记录个数："+sameCompanyList.size());
        result = false;
      }

    }
    return result;
  }


  /**
   * 获取老运营公司服务包关联关系列表
   * @return
   */
  public static List<Map> getOldCompanyServiceRelList(Long tenantId,Long companyId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select "+tenantId+" as group_id, t.* from bss_contact_service_rel t where t.company_id = ").append(companyId);
    logger.info("执行老运营公司服务包关联关系获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营公司服务包关联关系列表
   * @return
   */
  public static Map<String,Map> getNewCompanyServiceRelMap(Long tenantId,Long companyId,LogMain logger) throws Exception {
    Map<String,Map> newCompanyServiceRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_company_service_rel t where t.company_id = ").append(companyId);
    logger.info("执行新运营公司服务包关联关系获取，执行SQL: "+sql.toString());
    List<Map> newCompanyServiceRelList = NewBssDB.getSqlResult(sql.toString());
    for (Map newCompanyServiceRelMap : newCompanyServiceRelList){
      newCompanyServiceRelMapMap.put(String.valueOf(newCompanyServiceRelMap.get("id")),newCompanyServiceRelMap);
    }
    return newCompanyServiceRelMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareCompanyServiceRelWithNew(List<Map> oldCompanyServiceRelList,Long tenantId,Long companyId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newCompanyServiceRelMapMap  = getNewCompanyServiceRelMap(tenantId,companyId,logger);
    for (Map oldCompanyServiceRelMap : oldCompanyServiceRelList){
      System.out.println("开始执行公司服务包关联关系比对 公司服务包关联关系ID:"+oldCompanyServiceRelMap.get("id"));
      logger.info("开始执行公司服务包关联关系比对 公司服务包关联关系ID:"+oldCompanyServiceRelMap.get("id"));
      //查询新运营对应功能集
      Map newCompanyServiceRelMap =newCompanyServiceRelMapMap.get(String.valueOf(oldCompanyServiceRelMap.get("id")));
      if (newCompanyServiceRelMap == null){
        logger.warn("公司服务包关联关系ID:"+oldCompanyServiceRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同公司服务包关联关系ID,继续执行数据信息比对 新运营公司服务包关联关系："+ JSON.toJSONString(newCompanyServiceRelMap));
        //新老数据比对： tenant_id company_id service_package_id status
        oldCompanyServiceRelMap.put("tenant_id",oldCompanyServiceRelMap.get("group_id"));
        //oldCompanyServiceRelMap.put("status",oldCompanyServiceRelMap.get("contract_audit_status"));
        result = ServiceCommon.compareMapValue(oldCompanyServiceRelMap,newCompanyServiceRelMap,
                Lists.newArrayList("tenant_id","company_id","service_package_id"),
                "公司服务包关联关系ID:"+oldCompanyServiceRelMap.get("id"),
                logger);
      }

      //重复公司服务包关联关系检查
      List<Map> sameCompanyServiceRelList = NewBssDB.getSqlResult("select * from bss_company_service_rel where id != "+oldCompanyServiceRelMap.get("id")
              +" and tenant_id = "+oldCompanyServiceRelMap.get("group_id")
              +" and company_id = "+oldCompanyServiceRelMap.get("company_id")
              +" and service_package_id = "+oldCompanyServiceRelMap.get("service_package_id"));
      if (!CollectionUtils.isEmpty(sameCompanyServiceRelList)){
        logger.error("老运营公司服务包关联关系ID:"+oldCompanyServiceRelMap.get("id")+" 新运营存在重复公司服务包关联关系记录个数："+sameCompanyServiceRelList.size());
        result = false;
      }
    }
    return result;
  }


}
